<?php

namespace App\Admin\Controllers;

use App\Admin\BaseAdmin;
use App\Http\Controllers\Controller;
use App\Models\AdminUser;
use App\Models\MoneyLog;
use App\User;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Box;
use Illuminate\Http\Request;

class UserController extends Controller
{
    use ModelForm, BaseAdmin;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('用户');
            $content->description('管理');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('编辑');
            $content->description('');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('创建');
            $content->description('');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(User::class, function (Grid $grid) {
            $grid->model()->orderBy("id","desc");
            $grid->id('ID')->sortable();
            $grid->column('avatar', '头像')->thumb(40, 40);
            $grid->column('name', '昵称');
            $grid->sex('性别')->display(function () {
                $gender = $this->wx_user->country ?? '';
                $gender = $gender ? '男' : '女';
                return $gender;
            })->label('default');;
            $grid->city('地区')->display(function () {
                $city = $this->wx_user->city ?? '未知';
                $province = $this->wx_user->province ?? '火星';
                return "{$province} {$city}";
            })->label('default');
            $grid->column('phone', '手机');
            $grid->column('money', '余额')->label("default");
            $grid->column('sum_money', '总收入')->label("default");
            $grid->column('liveroom_sum_money', '直播间收入')->label("default");
            $grid->column('tg_sum_money', '推广收入')->label("default");
            $grid->column('created_at','注册时间');
            $grid->disableRowSelector();
            $grid->disableCreateButton();
            $grid->disableExport();
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableDelete();
                $actions->disableEdit();
                $actions->append("<a href='" . route('user-edit_money', ['id' => $actions->getKey()]) . "' class='btn btn-xs btn-default'>调整余额</a>");
            });

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(User::class, function (Form $form) {

            $form->display('id', 'ID');

            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }

    public function edit_money(Request $request)
    {
        $id = $request->input("id");
        $user = User::query()->findOrFail($id);
        if ($request->isMethod('post')) {
            $money = $request->input("set_money", 0);
            $desc = $request->input("desc", '平台调整');
            if ($money == 0) {
                return $this->error('金额不能为0');
            }
            $log = MoneyLog::addLog($user, $money, MoneyLog::ADMIN, $desc, AdminUser::class, Admin::user()->id);
            if ($log) {
                return $this->success('调整成功，流水ID:' . $log->id);
            } else {
                return $this->error('跳转失败');
            }

        } else {
            return Admin::content(function (Content $content) use ($user) {
                $content->header('调整余额');
                $form = new \Encore\Admin\Widgets\Form();
                $form->display('id', '用户ID')->default($user->id);
                $form->display('name', '用户ID')->default($user->name);
                $form->display('money', '当前余额')->default($user->money);
                $form->display('sum_money', '总收入')->default($user->sum_money);
                $form->number('set_money', '金额')->help('加钱正数，扣钱负数');
                $content->body(new Box('金额设置', $form));
            });
        }

    }
}
